# -*- coding: utf-8 -*-
"""
@ Created on 2024-06-14 12:23
---------
@summary: 统一响应模型
---------
@author: Lsaiah
"""

from typing import Any, Optional, Dict, List
from pydantic import BaseModel
from constant import *


class ResponseModel(BaseModel):
    """统一响应模型"""
    code: int
    message: str
    data: Optional[Any] = None
    timestamp: Optional[str] = None

    @classmethod
    def success(cls, data: Any = None, message: str = MESSAGE_SUCCESS, code: int = CODE_SUCCESS):
        """成功响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=data,
            timestamp=datetime.now().isoformat()
        )

    @classmethod
    def error(cls, message: str = MESSAGE_ERROR, code: int = CODE_ERROR, data: Any = None):
        """错误响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=data,
            timestamp=datetime.now().isoformat()
        )

    @classmethod
    def not_found(cls, message: str = MESSAGE_NOT_FOUND, code: int = CODE_NOT_FOUND):
        """资源不存在响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=None,
            timestamp=datetime.now().isoformat()
        )

    @classmethod
    def unauthorized(cls, message: str = MESSAGE_UNAUTHORIZED, code: int = CODE_UNAUTHORIZED):
        """未授权响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=None,
            timestamp=datetime.now().isoformat()
        )

    @classmethod
    def forbidden(cls, message: str = MESSAGE_FORBIDDEN, code: int = CODE_FORBIDDEN):
        """禁止访问响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=None,
            timestamp=datetime.now().isoformat()
        )

    @classmethod
    def validation_error(cls, message: str = MESSAGE_DATA_VALIDATION_ERROR, code: int = CODE_VALIDATION_ERROR):
        """数据验证错误响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            data=None,
            timestamp=datetime.now().isoformat()
        )


class PaginationModel(BaseModel):
    """分页模型"""
    page: int
    size: int
    total: int
    total_pages: int
    list: List[Any] = []

    @classmethod
    def create(cls, list_data: List[Any], page: int, size: int, total: int):
        """创建分页响应"""
        total_pages = (total + size - 1) // size  # 向上取整
        return cls(
            page=page,
            size=size,
            total=total,
            total_pages=total_pages,
            list=list_data
        )


class ListResponseModel(BaseModel):
    """列表响应模型"""
    code: int
    message: str
    data: Optional[PaginationModel] = None
    timestamp: Optional[str] = None

    @classmethod
    def success(cls, list_data: List[Any], page: int, size: int, total: int, message: str = MESSAGE_DATA_QUERY_SUCCESS):
        """成功列表响应"""
        from datetime import datetime
        pagination = PaginationModel.create(list_data, page, size, total)
        return cls(
            code=CODE_SUCCESS,
            message=message,
            data=pagination,
            timestamp=datetime.now().isoformat()
        )


class TokenResponseModel(BaseModel):
    """令牌响应模型"""
    access_token: str
    refresh_token: str
    token_type: str = "bearer"
    expires_in: int
    user_info: Dict[str, Any]

    @classmethod
    def create(cls, access_token: str, refresh_token: str, expires_in: int, user_info: Dict[str, Any]):
        """创建令牌响应"""
        return cls(
            access_token=access_token,
            refresh_token=refresh_token,
            expires_in=expires_in,
            user_info=user_info
        )


class LoginResponseModel(BaseModel):
    """登录响应模型"""
    code: int
    message: str
    data: Optional[TokenResponseModel] = None
    timestamp: Optional[str] = None

    @classmethod
    def success(cls, access_token: str, refresh_token: str, expires_in: int, user_info: Dict[str, Any], message: str = MESSAGE_USER_LOGIN_SUCCESS):
        """登录成功响应"""
        from datetime import datetime
        token_data = TokenResponseModel.create(access_token, refresh_token, expires_in, user_info)
        return cls(
            code=CODE_USER_LOGIN_SUCCESS,
            message=message,
            data=token_data,
            timestamp=datetime.now().isoformat()
        )


class ErrorResponseModel(BaseModel):
    """错误响应模型"""
    code: int
    message: str
    detail: Optional[str] = None
    timestamp: Optional[str] = None

    @classmethod
    def create(cls, code: int, message: str, detail: Optional[str] = None):
        """创建错误响应"""
        from datetime import datetime
        return cls(
            code=code,
            message=message,
            detail=detail,
            timestamp=datetime.now().isoformat()
        )
